﻿using System;
using System.Collections.Generic;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Epitech.Krisk.Xna.ScreenManagerLib.Widget
{
    public class ComboBox : MenuEntry
    {
        LayoutEntry _list;

        private List<String> _elements;

        public List<String> Elements
        {
            get { return _elements; }
            set
            {
                _elements = value;
                text = _elements[0];
            }
        }

        int currentInList = 0;

        private Texture2D _fleche;

        public override void LoadContent(GameScreen currentScreen)
        {
            _fleche = currentScreen.ScreenManager.Game.Content.Load<Texture2D>("Game/ComboBox");
            base.LoadContent(currentScreen);
        }

        private void ConstructList()
        {
            if (_list == null)
            {
                Vector2 pos = new Vector2(Parent.Position.X, Position.Y);

                _list = new LayoutEntry(pos);
            }
            int idx = currentInList;

            if (_list.SelectedIndex >= _affElem - 1)
                idx -= _affElem - 1;

            idx = idx < 0 ? 0 : idx + _affElem < _elements.Count ? idx : _elements.Count - _affElem;

            _list.MenuEntries.Clear();
            if (_elements != null && _elements.Count > 0)
            {
                for (int i = 0; i < _affElem; i++)
                {
                    MenuEntry elem = new MenuEntry(_elements[i + idx])
                                         {
                                             DefaultColor = DefaultColor,
                                             FontSize = FontSize,
                                             IsPulse = IsPulse,
                                             SelectedColor = SelectedColor
                                         };
                    elem.Selected += OnSelect;
                    _list.AddEntry(elem);
                }
            }
        }

        public String SelectedElem
        {
            get { return text; }
            set { text = value;
                currentInList = Elements.IndexOf(value);
            }
        }

        private int _affElem;

        public ComboBox(int affElem = 3)
            : base("...")
        {
            _affElem = affElem;
            Selected += OnSelect;
        }

        bool _unrolling;

        private void OnSelect(object sender, PlayerIndexEventArgs e)
        {
            if (_unrolling)
            {
                _unrolling = false;
                Parent.Active = true;
                text = _list.SelectedEntry.Text;
            }
            else
            {
                _unrolling = true;
                Parent.Active = false;
                ConstructList();
            }
        }

        public override void Update(GameScreen screen, bool isSelected, GameTime gameTime)
        {
            if (_unrolling)
            {
                if (_list != null)
                {
                    _list.UpdateEntryLocation(screen);
                    _list.Update(gameTime, screen, false, false);
                }
            }
            else
                base.Update(screen, isSelected, gameTime);
        }

        int lastidx = 0;

        public override void HandleInput(GameScreen currentScreen, InputState input)
        {
            if (isSelect)
            {
                if (_unrolling && _list != null)
                {
                    PlayerIndex tmp;
                    LayoutEntry.LayoutOutput output = _list.HandleInput(currentScreen, input);
                    int op = _list.SelectedIndex - lastidx;

                    currentInList += op;
                    if (currentInList >= Elements.Count)
                        currentInList = Elements.Count - 1;
                    else if (currentInList < 0)
                        currentInList = 0;
                    if (output != LayoutEntry.LayoutOutput.None && op != 0)
                    {
                        ConstructList();
                        _list.UpdateEntryLocation(currentScreen);
                    }
                    if (output == LayoutEntry.LayoutOutput.Begin)
                        _list.SelectedIndex = 0;
                    else if (output == LayoutEntry.LayoutOutput.End)
                        _list.SelectedIndex = _affElem - 1;
                    lastidx = _list.SelectedIndex;
                    if (input.IsNewKeyPress(Keys.Escape, null, out tmp)
                        || input.IsNewButtonPress(Buttons.Start, null, out tmp))
                        _unrolling = !_unrolling;
                }
            }
        }

        public override void Draw(GameScreen screen, bool isSelected, GameTime gameTime)
        {
            SpriteBatch sp = screen.ScreenManager.SpriteBatch;
            if (_unrolling)
            {
                if (_list != null)
                    _list.Draw(gameTime, screen);
            }
            else
                base.Draw(screen, isSelected, gameTime);

            Vector2 pos;

            pos.X = Position.X + screen.ScreenManager.Font.MeasureString(SelectedElem).X + 25;
            pos.Y = Position.Y - _fleche.Height / 2;

            sp.Draw(_fleche, pos, Color.White);
        }
    }
}
